<?php

/* Zend_Registery */
require_once ( 'Zend/Registry.php' );

/* Zend_Controller_Plugin_Abstract */
require_once ( 'Zend/Controller/Plugin/Abstract.php' );

class Gpc_Controller_Plugin extends Zend_Controller_Plugin_Abstract {

   private $user;

   private $db;

   private $session;

   private $config;

   public function routeStartup( Zend_Controller_Request_Abstract $response ) {

      /* Starting up a Namespace using Zend's Session management */
      require_once ( 'Zend/Session/Namespace.php' );
      $this->session = new Zend_Session_Namespace( );
      
      // Base config from within the ini file.
      require_once ( 'Zend/Config/Ini.php' );
      $this->config = new Zend_Config_Ini( CONFIG, ENVIRONMENT );
      
      /* Zend_Db */
      require_once ( 'Zend/Db.php' );
      $this->db = Zend_Db::factory( $this->config->database );
      
      // TODO: Add config caching.

      /* Store values into the registry aka session */
      Zend_Registry::set( 'config', $this->config );
      Zend_Registry::set( 'db', $this->db );
   
   }

   public function dispatchLoopStartup( Zend_Controller_Request_Abstract $response ) {

      /* Config Model class */
      require_once ( 'Config.php' );
      $site_config = new stdClass( );
      $tmp = new Config( );
      foreach ( $tmp->fetchAll()->toArray() as $row ) {
         
         if ( ! isset($site_config->{$row['category']}) ) {
            $site_config->{$row['category']} = new stdClass( );
         }
         
         $site_config->{$row['category']}->{$row['option']} = $row['value'];
      }
      
      Zend_Registry::set( 'site_config', $site_config );
      
      /* 
       * Start User object for authentication and authorization
       *   Supports SMF, phphp2, phpBB3 vB and built in method 
       */
      require_once ( 'Gpc/User.php' );
      $this->user = Gpc_User::factory( $site_config->forum->software );
      $this->user->loadUserSession(); // Checks for and loads a user's session
      

      // Load up the veiew object so that we can set whether a user is logged in or not.
      // Seems a bit odd to call "MVC" via the layout. Shouldn't this be done via the controller?
      //   -- Discussed with Zend, fubar. Nobody voiced an opinion, to late to fix now. 
      require_once ( 'Zend/Layout.php' );
      $layout = Zend_Layout::startMvc();
      $layout->setLayout( 'default' );
      
      $view = $layout->getView();
      
      /* So view files have access to the view specific config variabes */
      $view->config = $site_config->view;
      
      /* Zend_Auth */
      require_once ( 'Zend/Auth.php' );
      if ( Zend_Auth::getInstance()->hasIdentity() ) { /* User is logged in */
         $view->user = Zend_Auth::getInstance()->getIdentity();
         $view->loggedIn = true;
      } else { /* User is not logged in */
         $view->loggedIn = false;
      }
      
      /* If we're in the admin module, insure the user is logged in and autheorized */
      if ( $this->getRequest()->getModuleName() === "admin" ) {
         /* "error" check is to prevent against redirect loops */
         if ( $view->loggedIn !== true && $this->getRequest()->getControllerName() !== "error" ) {
            $this->session->referalUrl = $_SERVER['REQUEST_URI']; // So they can at least return to the page they once came from
            Zend_Controller_Action_HelperBroker::getStaticHelper( 'redirector' )->goto( null, 'login', '' );
         }
         
         // TODO: Code authorization check, make sure they are allowed to access admin pages.
         //           -- This will use the User factory object.

      }
      
      /* Site wide javascript and css */
      $view->headScript()->prependFile( Zend_Registry::get( 'site_config' )->js_url . "main.js" );
      $view->headLink()->appendStylesheet( Zend_Registry::get( 'site_config' )->css_url . "main.css" );
      
      /* Store objects in the register for use throughout the application */
      Zend_Registry::set( 'session', $this->session );
      Zend_Registry::set( 'user', $this->user );
   }
}
